package org.dyk.seamanshousefrontdesk.controller;

import org.dyk.seamanshousefrontdesk.model.Goods;
import org.dyk.seamanshousefrontdesk.service.goods.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Collections;
import java.util.List;

@Controller
public class IndexController {
    @Autowired
    private GoodsService goodsService;

    /**
     * 商城主页
     * @param model
     * @return
     */
    @RequestMapping(value = {"/", "/index"})
    public String index(Model model) {
        //获取所有商品
        List<Goods> all = goodsService.findAll();
        Collections.shuffle(all);
        model.addAttribute("goodsAll",all);
        //获取所有商品
        List<Goods> goods = goodsService.findAll();
        Collections.shuffle(goods);
        model.addAttribute("allGoods",goods);
        return "/index";
    }

    /**
     * 商品详情页面
     * @param gid
     * @param model
     * @return
     */
    @RequestMapping("/proDetails")
    public String proDetails(@RequestParam int gid, Model model){
        Goods byGid = goodsService.findByGid(gid);
        model.addAttribute("goods",byGid);
        return "proDetails";
    }
}
